<?php
namespace app\api\controller\goods;

use app\common\controller\Api;

class Goods extends Api
{
    protected $noNeedLogin = ['goodsDetail','collection'];
    protected $noNeedRight = ['*'];


    /**
     * 产品详情
     * @path api/goods/Goods/goodsDetail
     */
    public function goodsDetail()
    {
        $userId = $this->auth->id;
        $storeId = $this->auth->store_id;
        $goodsId = $this->request->param('goods_id');
        $goods = db('goods')->where('id',$goodsId)->find();
        $store = db('store')->where('id',$storeId)->find();
        $notGoods = explode(',',$store['not_goods']);
        if (in_array($goodsId,$notGoods) || $goods['is_on_sale'] < 1) {
            $backList = [
                'noSale'=>1,
                'store'=>[],
                'userRole'=>$this->auth->type,
                'goods'=>[],
                'active'=>[],
            ];
            $this->success('请求成功',$backList);
        }

        $todayDay = date('Y-m-d H:i:s');


        if (empty($goods)) {
            $goods = [];
        } else {
            $kucun = db('store_goods')->where(['store_id'=>$storeId,'goods_id'=>$goodsId])->find();
            $goods = goodsPriceReset($goods,$this->auth->type);
            $goods = addWebSiteUrl($goods,['image']);
            $isCollect = db('user_collect')->where(['user_id'=>$userId,'goods_id'=>$goodsId])->find();
            $goods['content'] = get_image_url($goods['content'],protocol_name() . '://' . $_SERVER['SERVER_NAME']);
            $goods['kucun'] = isset($kucun['stock']) ? $kucun['stock'] : 0;
            $goods['is_collect'] = $isCollect ? 1 : 0;
            $goodsImages = explode(',',trim($goods['goods_images']));
            $goodsImagesArr = [];
            foreach ($goodsImages as $key => $value) {
                $goodsImagesArr[] = addWebSiteUrl(['goods_images'=>$value],['goods_images']);
            }
            $goods['goods_images'] = $goodsImagesArr;
            $goods['start_sale'] = isset($kucun['start_sale']) ? $kucun['start_sale'] : $goods['start_sale'];
            $goods['cashsale'] = $goods['salecount']+$goods['cashsale'];
        }
        $active = db('store_active')
            ->where([
                'store_id'=>$storeId,
                'status'=>1,
                'authen'=>2
            ])->where("starttime<='{$todayDay}' and endtime > '{$todayDay}'")
            ->select();

        $storeName = db('store')->field('store_name,avatar')->where('id',$storeId)->find();
        $storeName = addWebSiteUrl($storeName,['avatar']);

        $backList = [
            'noSale'=>0,
            'store'=>$storeName,
            'userRole'=>$this->auth->type,
            'goods'=>$goods,
            'active'=>$active,
        ];

        $this->success('请求成功',$backList);
    }

    /**
     * 商品收藏
     * @path api/goods/Goods/collection
     */
    public function collection()
    {
        $userId = $this->auth->id;
        $storeId = $this->auth->store_id;
        $goodsId = $this->request->param('goods_id');
        $msg = '收藏成功';
        $collect = db('user_collect')->where([
            'goods_id'=>$goodsId,
            'user_id' => $userId
        ])->find();
        if (empty($collect)) {
            db('user_collect')->insert([
                'goods_id'=>$goodsId,
                'store_id'=>$storeId,
                'user_id' => $userId,
                'addtime' => time()
            ]);
        } else {
            db('user_collect')->where([
                'goods_id'=>$goodsId,
                'user_id' => $userId,
                'store_id' => $storeId
            ])->delete();
            $msg = "已取消收藏";
        }
        $this->success($msg);
    }
}
